package com.applink.sell_proof.utilXMLParser;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.content.Context;

import com.applink.sell_proof.share.Parameter;

public class DesignFormatParser {
	
  
	public static ArrayList<ColumnSetting> ParserIntoColumnSetting(Context paramContext, String xmlfilename, String tagname) {

		ArrayList<ColumnSetting> columnSettingList = new ArrayList<ColumnSetting> ();
		
        try {
        	
        	InputStream istream = paramContext.getAssets().open(xmlfilename);
            InputSource isource = new InputSource(new InputStreamReader(istream,  "UTF-8"));
            
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(isource);
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName(tagname);
            
            for (int temp = 0; temp < nList.getLength(); temp++) {
         
            	Node nNode = nList.item(temp);
            	ColumnSetting columnsetting = new ColumnSetting();
            	
            	//set column type
                if (nNode.hasAttributes()) {
                    Attr attr = (Attr) nNode.getAttributes().getNamedItem(Parameter.COL_TYPE);
                    if (attr != null) {
                        String colType= attr.getNodeValue();                      
                        columnsetting.setColType(colType);         
                    }
                }
                
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                	String content ="";
                	Element element = (Element) nNode;
                	
                	//set mapping name
                	NodeList nodes = element.getElementsByTagName(Parameter.COL_MAPPINGNAME);
                	if (nodes.getLength() !=0){
                		content = getTextNodeValue(nodes.item(0));
                		columnsetting.setMappingName(content);  
                	}
                	
                	//set linking mapping name
                	nodes = element.getElementsByTagName(Parameter.COL_LINKINGMAPPINGNAME);
                	if (nodes.getLength() !=0){
                		content = getTextNodeValue(nodes.item(0));
                		columnsetting.setLinkMappingName(content);    
                	}
                	
                	//set linkpage
                	nodes = element.getElementsByTagName(Parameter.COL_LINKPAGE);
                	if (nodes.getLength() !=0){
                		content = getTextNodeValue(nodes.item(0));
                		columnsetting.setLinkPage(content);  
                	}
                	
                	//set header
                	nodes = element.getElementsByTagName(Parameter.COL_HEADER);
                	if (nodes.getLength() !=0){
                		content = getTextNodeValue(nodes.item(0));
                		columnsetting.setHeaderText(content);   
                	}
                }
                columnSettingList.add(columnsetting);
            }
        }catch (Exception e) {
            System.out.println("Catch");
            e.printStackTrace();
        }
        return columnSettingList;
	}
	
	private static String getTextNodeValue(Node node) {
        Node child;
        if (node != null) {
            if (node.hasChildNodes()) {
                child = node.getFirstChild();
                while(child != null) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                    child = child.getNextSibling();
                }
            }
        }
        return "";
    }
}
